<?php

/*
 * CopyRight  : (C)2012-2099 HaoTeam Inc.
 * Document   : Timers.php
 * Created on : 2021-6-8 9:55:16
 * Author     : Tiger <1192851302@qq.com>
 * Description: This is NOT a freeware, use is subject to license terms.
 *              这即使是一个免费软件,使用时也请遵守许可证条款,得到当时人书面许可.
 *              未经书面许可,不得翻版,翻版必究;版权归属 HaoTeam Inc;
 */

namespace haoteam\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Log;
use Workerman\Worker;
use Workerman\Lib\Timer;

/**
 * 系统定时服务
 * Class Timers
 * Tiger <1192851302@qq.com> 2021-6-8 9:55:16
 */
class Timers extends Command {

    /**
     * @var int
     */
    protected $timer;

    /**
     * @var int|float
     */
    protected $interval = 2;

    protected function configure() {
        // 指令配置
        $this->setName('Timers')
                ->addArgument('status', Argument::REQUIRED, 'start/stop/reload/status/connections')
                ->addOption('d', null, Option::VALUE_NONE, 'daemon（守护进程）方式启动')
                ->addOption('i', null, Option::VALUE_OPTIONAL, '多长时间执行一次,可以精确到0.001', 2)
                ->setDescription('start/stop/restart Timers Service');
    }

    protected function init(Input $input, Output $output) {
        global $argv;
        if ($input->hasOption('i')) {
            $this->interval = floatval($input->getOption('i'));
        }
        var_dump($this->interval);
        var_dump($input->getOption('d'));
        var_dump($input->getArgument('status'));
        $argv[1] = $input->getArgument('status') ?: 'start';
        if ($input->hasOption('d')) {
            $argv[2] = '-d';
        } else {
            unset($argv[2]);
        }
    }

    protected function execute(Input $input, Output $output) {
        $this->init($input, $output);
        Worker::$pidFile = app()->getRuntimePath() . 'Timers.pid';
        Worker::$logFile = app()->getRuntimePath() . 'Timers.log';
        $task = new Worker();
        $task->count = 1;
        $task->onWorkerStart = [$this, 'start'];
        $task->runAll();
    }

    public function stop() {
        var_dump('stop 这里?');
        Timer::del($this->timer);
    }

    public function start() {
        $last = time();
        $task = [6 => $last, 10 => $last, 30 => $last, 60 => $last, 180 => $last, 300 => $last, 1800 => $last];
        $this->timer = Timer::add($this->interval, function () use (&$task) {
                    try {
                        $now = time();
                        event('Task_2');
                        foreach ($task as $sec => $time) {
                            if ($now - $time >= $sec) {
                                event('Task_' . $sec);
                                $task[$sec] = $now;
                            }
                        }
                    } catch (\Throwable $e) {
                        Log::error('定时器执行错误' . $e->getMessage().$e->getFile().$e->getLine());
                    }
                });
    }

}
